<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>部门信息列表</title>
  <link rel="stylesheet" href="${ctx}/static/admin/layui/css/layui.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/common.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/treeGrid.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/sidebar.css">
  </head>
  <body>
    <div class="layui-card-body">
	  <button class="layui-btn layui-btn-sm" id="add">添加</button>
	  <table class="layui-hide" id="dept_table"></table>
	</div>
  </body>
</html>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<script src="${ctx}/static/admin/js/common.js"></script>
<script type="text/html" id="enabled">
  <input type="checkbox" name="status" lay-filter="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="toolbar">
  <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
  <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">编辑</a>
</script>
<script type="text/javascript">
var tableIns;
layui.use(['layer','jquery','element','form','laytpl','treeGrid'], function(){
	var $ = jQuery = layui.$;
	var treeGrid = layui.treeGrid;
	var layer = layui.layer;
	var laytpl = layui.laytpl;
	var element = layui.element;
    var form = layui.form;
	
    tableIns = treeGrid.render({
        id: 'dept_table',
        elem: '#dept_table',
        idField: 'id',
        size:'sm',
        url: '${ctx}/sys_admin/sys/dept/getTreeGridView',
        height:'full-70',
        cellMinWidth: 80,
        treeId: 'id',//树形id字段名称
        treeUpId: 'pId',//树形父id字段名称
        treeShowName: 'name',//以树形式显示的字段
        cols: [[
            {type: 'numbers'},
            {field: 'id', title: 'ID',hide:true},
            {field: 'name', width: 300, title: '部门名称'},
            {field: 'seq', width: 80, title: '排序'},
            {field: 'status', width: 100, title: '状态', templet: '#enabled'},
            {field: 'des', unresize: true, title: '描述'},
            {width: 120 ,title: '操作', align: 'center', toolbar: '#toolbar'}
        ]],
        page:false
    });

    treeGrid.on('tool',function (obj) {
    	var data = obj.data;
    	if(obj.event === 'edit'){
	    	parent.layer.open({
	  		  	type: 2,
	  		  	area: ['700px', '350px'],
	  		  	fixed: false, //不固定
	  		  	maxmin: true,
	  		  	content: '${ctx}/sys_admin/sys/dept/form?id='+data.id,
		  		btn: ['保存', '关闭'],
	  	  		yes: function(index, layero){
		  	  		var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
			  		iframeWin.contentWindow.treeGrid = treeGrid;
	                iframeWin.contentWindow.treeId = 'dept_table';
			  		layero.find('iframe').contents().find('#submit-btn').click();
			  	},
			  	btn2: function(index, layero){
		  			var index = layer.getFrameIndex(window.name);
					layer.close(index); //再执行关闭   
			  	}
	  		});
        }else if(obj.event==="add"){//添加行
        	parent.layer.open({
  	  		  	type: 2,
  	  		  	area: ['700px', '350px'],
  	  		  	fixed: false, //不固定
  	  		  	maxmin: true,
  	  		  	content: '${ctx}/sys_admin/sys/dept/form?id='+data.id+'&isadd=true',
  	  		  	btn: ['保存', '关闭'],
	  	  		yes: function(index, layero){
		  	  		var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
			  		iframeWin.contentWindow.treeGrid = treeGrid;
	                iframeWin.contentWindow.treeId = 'dept_table';
			  		layero.find('iframe').contents().find('#submit-btn').click();
			  	},
			  	btn2: function(index, layero){
		  			var index = layer.getFrameIndex(window.name);
					layer.close(index); //再执行关闭   
			  	}
  	  		});
        }
    });
    
    form.on('switch(status)', function(data){
		var selectIfKey = data.othis;                                                
		var parentTr = selectIfKey.parents("tr");                                 
		var parentTrIndex = parentTr.attr("data-index");                          
		var id = parentTr.find(('td:eq(1)')).text().trim(); 
		var visible = 0;
		if(data.elem.checked){visible = 1;}
		$.post('${ctx}/sys_admin/sys/dept/visible', {id: id, visible: visible}, function() {}, 'json');
	}); 
    
    $("#add").on('click', function() {
    	parent.layer.open({
			type: 2,
			area: ['700px', '350px'],
			fixed: false, //不固定
			maxmin: true,
			content: '${ctx}/sys_admin/sys/dept/form?isadd=true',
	  		btn: ['保存', '重置', '关闭'],
		  	yes: function(index, layero){
		  		var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
		  		iframeWin.contentWindow.treeGrid = treeGrid;
                iframeWin.contentWindow.treeId = 'dept_table';
		  		layero.find('iframe').contents().find('#submit-btn').click();
		  	},
		  	btn2: function(index, layero){
		  		layero.find('iframe').contents().find('#reset-btn').click();
		  		return false;
		  	},
		  	btn3: function(index, layero){
	  			var index = layer.getFrameIndex(window.name);
				layer.close(index); //再执行关闭   
		  	}
		});
	});
});
</script>